<?php
namespace app\admin\controller;

class Goods extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='') $map[] = ['and','a.title','like',"%{$d['keyword']}%"];
            if($d['rec_id']!='') $map[] = ['and','a.rec_id','like',"%,{$d['rec_id']},%"];
            if($d['status']!='') $map[] = ['and','a.status','=',$d['status']];
            // if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            // if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            if($d['cat_id']!=''){
                if($d['type']){
                    $ids = [$d['cat_id']];
                }else{
                    $ids = m('GoodsCat','c')->getAllchildCat($d['cat_id']);
                    $ids[] = $d['cat_id'];
                }
                $map[] = ['and', 'a.cat_id','in', $ids];
            }
            $w  = where($map);
            $sql = "
            select
                a.*,b.cat_title ptitle
            from
                goods a
            left join
                goods_cat b
            on
                a.cat_id = b.id
            $w[0]
            order by a.sort desc,a.id desc".limit($d['page'],$d['limit']);

            $rs = $this->db->query($sql, $w[1],'',true);
            if($rs){//,c.title ctitle
                $rule = [
                    'status'=>function($v){return c("type.goods_status.$v");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                //查询所有推荐位
                $rec_list = $this->db->query('select * from goods_rec order by sort desc');
                foreach ($data as $key => $val) {
                    $name = "";
                    $ids = explode(',',$val['rec_id']);
                    foreach ($ids as $rec_id) {
                        foreach ($rec_list as $k1 => $v1) {
                            if($v1['id'] == $rec_id){
                                $name .= $v1['title'] . "|";
                            }
                        }
                    }
                    $data[$key]['ctitle'] = rtrim($name,"|");
                }
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $rec = $this->db->query("select title,id from goods_rec order by sort desc,id desc");
        $cat = m('GoodsCat','c')->allSubCat();
        return $this->fetch(['rec'=>$rec,'cat'=>$cat]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
                'price' => "require",
            ];
            $msg = [
                'title.require' => '商品名称不能为空',
                'price.require' => '商品价格不能为空',
            ];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $d['shop_id'] = 1;
            $rs = m("goods", "c")->add($d);
            if ($rs) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err(m("goods", "c")->getError());
            }
        }
        $cat_id = $this->get('cat_id');
        $cat_list  = m("goodsCat","c")->allSupCat($cat_id);
        $spec_list = m("goodsSpec","c")->catSpecList($cat_id);
        $d = [
            "spec_list"=>$spec_list,
            "cat_list"=>$cat_list
        ];
        $this->fetch($d);
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
                'price' => "require",
            ];
            $msg = [
                'title.require' => '商品名称不能为空',
                'price.require' => '商品价格不能为空',
            ];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $goods_info = $this->db->find('goods',$d['id']);
            $d['shop_id'] = $goods_info['shop_id'];
            $goods_model = m("goods", "c");
            $rs = $goods_model->edit($d);
            if ($rs) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $d = $this->get();
        $goods_info = $this->db->find('goods',$d['id']);
        $goods_info['content'] = htmlspecialchars_decode($goods_info['content']);
        $goods_info['spec']    = htmlspecialchars_decode($goods_info['spec']);
        $goods_info['pics']    = explode(",",$goods_info["pics"]);

        //规格双引号处理
        $goods_cat_model = m("goodsCat","c");
        if(isset($d['cat_id'])){
            $goods_info['cat_id'] = $d['cat_id'];
        }
        //分类
        $cat = m('GoodsCat','c')->allSubCat();
        //t($cat);
        //规格
        $spec = json_decode($goods_info['spec'],true);
        foreach ($spec as $k=>$v){
            $spec[$k] = implode(",",$v);
        }
        //商品规格预览
        $goods_spec_list = $this->db->query("select * from goods_price where goods_id = {$goods_info['id']}");
        if($goods_spec_list){
            foreach ($goods_spec_list as $k=>$v){
                $goods_spec_list[$k]['spec'] = explode("_",$v['title']);
            }
        }
        $this->fetch(["goods_info"=>$goods_info,"cat"=>$cat,'spec'=>$spec,"goods_spec_list" => $goods_spec_list]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('goods',$d['id']);
            $this->db->delete('goods_attr',$d['id'],'goods_id');
            m('Attachment','xcom')->imgDel('goods',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','preload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('goods',$id);
                $this->db->delete('goods_attr',$id,'goods_id');
                m('Attachment','xcom')->imgDel('goods',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','preload');
            return suc();
      }
    }

    //详情
    public function detail($id){
        return $this->fetch();
    }

    //发布产品选择分类
    public function goodsCat(){
        $cat1 = $this->db->query("select * from goods_cat where parent_id=0 order by sort desc, id desc ");
        return $this->fetch(['cat1'=>$cat1]);
    }

    //修改上下架
    public function updateStatus(){
      if(is_post()){
            $d = $this->post;
            $status = $this->get('status');
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->update('goods',['status'=>$status],['id=?',[$id]]);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','reload');
            return suc();
      }
    }

    //设置推荐位
    public function setRecommend(){
        if(is_post()){
            $d = $this->post;
            $ids = explode(',',$d['goods_id']);
            $rec_id = isset($d['rec_id']) ? join(',',$d['rec_id'])."," : "";
            foreach ($ids as $k => $v) {
                $data = [
                    'rec_id'=> ",0,{$rec_id}"
                ];
                $where = [
                    "id=?",
                    [$v]
                ];
                $this->db->update('goods',$data,$where);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','preload');
            return suc();
        }

        $ids = $this->get("ids");
        //查询所有推荐位
        $list = $this->db->query('select * from goods_rec order by sort desc');
        return $this->fetch(['list'=>$list,'goods_id'=>join(',',$ids)]);
    }
}
